﻿cmake_minimum_required (VERSION 3.15)

# 禁用开发者警告
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE INTERNAL "No dev warnings")
# 设置默认构建类型为Release

set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)

# 设置目标程序
set(TARGET_SAMPLE pcltest)
project("PCLTEST")
# 设定编译选项
if (MSVC)
    option(OpenCV_STATIC "Using static opencv" OFF)
endif()


option(OPENGL_VIEW "Enable GLUT OpenGL point cloud view " OFF)

if (NOT TARGET tycam)
    # 仅编译示例程序
    option(OpenCV_STATIC OFF)
    set(INCLUDE_PATH include)
    include_directories(${INCLUDE_PATH})
    set(ABSOLUTE_TARGET_LIB tycam)
    add_library(${ABSOLUTE_TARGET_LIB} SHARED IMPORTED)
    if (MSVC)  # For Windows
        set(LIB_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/hostapp/)
        if(CMAKE_CL_64)  # x64
            set_property(TARGET ${ABSOLUTE_TARGET_LIB} PROPERTY IMPORTED_LOCATION ${LIB_ROOT_PATH}/x64/tycam.dll)
            set_property(TARGET ${ABSOLUTE_TARGET_LIB} PROPERTY IMPORTED_IMPLIB  ${LIB_ROOT_PATH}/x64/tycam.lib)
        else()
            set_property(TARGET ${ABSOLUTE_TARGET_LIB} PROPERTY IMPORTED_LOCATION ${LIB_ROOT_PATH}/x86/tycam.dll)
            set_property(TARGET ${ABSOLUTE_TARGET_LIB} PROPERTY IMPORTED_IMPLIB ${LIB_ROOT_PATH}/x86/tycam.lib)
        endif()
    else()
      if(ARCH)
          set_property(TARGET ${ABSOLUTE_TARGET_LIB} PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../lib/linux/lib_${ARCH}/libtycam.so)
      else()
          set(ABSOLUTE_TARGET_LIB -ltycam)
      endif()
    endif()
endif()

if (MSVC)
    if (OpenCV_STATIC)
        message("using static opencv")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
    endif()
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

# 编译公共库
set(COMMON_SOURCES common/MatViewer.cpp common/TYThread.cpp common/ImageSpeckleFilter.cpp common/DepthInpainter.cpp)
add_library(sample_common STATIC ${COMMON_SOURCES})
if(UNIX)
  target_link_libraries(sample_common pthread)
endif()
# 设置OpenCV的路径
set(OpenCV_DIR "D:/opencv/install/x64/vc16/lib")
# 查找OpenCV
find_package(OpenCV REQUIRED)
if (NOT OpenCV_FOUND)
    message(FATAL_ERROR "OpenCV library not found")
else()
    include_directories(${OpenCV_INCLUDE_DIRS})
    include_directories(${OpenCV2_INCLUDE_DIRS})
    link_directories(${OpenCV_LIB_DIR})
endif()
set(PCL_DIR "D:/Program Files/PCL/PCL 1.11.1/cmake")
find_package(PCL REQUIRED)
if(NOT PCL_FOUND)
    message(FATAL_ERROR "PCL library not found")
else()
    include_directories(${PCL_INCLUDE_DIRS})
    link_directories(${PCL_LIBRARY_DIRS})
    add_definitions(${PCL_DEFINITIONS})
endif()

# 设置3D点云映射库目录

# set(CLOUD_VIEWER_LIBRARY "D:/Program Files/TYCloudViewer/cloud_viewer.lib")
set(GLUT_glut_static_LIBRARY "D:/Program Files/freeglut/lib/freeglut_static.lib")
set(GLUT_glut_LIBRARY "D:/Program Files/freeglut/lib/freeglut.lib")
include_directories(common/cloud_viewer/freeglut/include)
file(GLOB CLOUD_VIEWER_SOURCES common/cloud_viewer/*.cpp)
file(GLOB CLOUD_VIEWER_HEADERS common/cloud_viewer/*.h)

# 设置并编译目标程序
file(GLOB sources src/*.cpp)
file(GLOB headers headers/*.h headers/*.hpp)

# add_executable(${TARGET_SAMPLE} ${sources} ${headers})
add_executable(${TARGET_SAMPLE} ${sources} ${headers} ${CLOUD_VIEWER_SOURCES} ${CLOUD_VIEWER_HEADERS})

add_dependencies(${TARGET_SAMPLE} sample_common ${TARGET_LIB})
# target_link_libraries(${TARGET_SAMPLE} sample_common ${ABSOLUTE_TARGET_LIB} ${OpenCV_LIBS} ${PCL_LIBRARIES})
target_link_libraries(${TARGET_SAMPLE} sample_common 
                    ${ABSOLUTE_TARGET_LIB} 
                    ${OpenCV_LIBS} 
                    ${PCL_LIBRARIES}
                    ${GLUT_glut_LIBRARY} 
                    ${GLUT_glut_static_LIBRARY} 
                    ${CLOUD_VIEWER_LIBRARY}
                    )


set_target_properties(${TARGET_SAMPLE} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/)
set_target_properties(${TARGET_SAMPLE} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON)

install(TARGETS ${TARGET_SAMPLE}
       RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
       LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
       ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
